package com.itheima.exception;

import com.itheima.common.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
public class ExceptionAdvice {
    @ExceptionHandler(BusinessException.class)
    public Result handlerException1(BusinessException e){
        if (e.getErrorCode() != null) {
            return Result.validateFailed();
        }
        return Result.validateFailed(e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public Result handlerException2(Exception e){
        e.printStackTrace();
        return Result.failed();
    }

    @ExceptionHandler(value = ApiException.class)
    public Result handle(ApiException e) {
        if (e.getErrorCode() != null) {
            return Result.failed(e.getErrorCode());
        }
        return Result.failed(e.getMessage());
    }
}

